package com.song.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.song.dao.BirthdayMapper;
import com.song.model.Birthday;
import com.song.pojo.Result;
import com.song.pojo.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 宋涛
 * @version 1.0
 * @date 2020/7/29 9:26
 */
@RestController
@RequestMapping("/birthady")
public class BirthdayControllor {
    @Autowired
    private BirthdayMapper birthdayMapper;
    @Autowired
    Snowflake snowflake;

    @PostMapping("/save")
    public ResultMsg add(@RequestBody Birthday birthday) {
        if (birthday == null) {
            return Result.error(1000, "数据不能为空！");
        }

        if (StringUtils.isEmpty(birthday.getId())) {
            String id = snowflake.nextIdStr();
            birthday.setId(id);
        }
        birthdayMapper.insertSelective(birthday);

        return Result.success(0);

    }

    @DeleteMapping
    public ResultMsg delete(String id ) throws IllegalAccessException {
        int i = birthdayMapper.deleteByPrimaryKey(id);
        if(i != 1 ){
            throw  new IllegalAccessException("删除失败");
        }
        return Result.success(0);

    }

//    根据用户查询这个月的生日情况
    public ResultMsg getBirthadyByUid(HttpServletRequest request){
        request.getHeaders("Authorization");

        return  Result.success();
    }


}
